package com.dungeon.game;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.util.Log;

import com.dungeon.game.Constants.Game_mode;
import com.dungeon.game.Party.Character;

public class Gamestate {

	static Party p;
	static World w;
	static Enemies e;

	static Item_factory i;
	static Monster_action_factory ma;
	static Character_action_factory ca;

	static ArrayList<String> sound_queue;
	static ArrayList<Msg> message_queue;
	static boolean quit_after_pause;
	static Game_mode mode;
	static Character dragging, actor;
	static double started_dragging;
	static boolean locked;
	static boolean actionchooser_created;
	static Character_action action;	
	static int flee_attempts;

	{init();}

	static void prep(){
		sound_queue = new ArrayList<String>();
		message_queue = new ArrayList<Msg>();
		quit_after_pause = false;
		locked = false;
		mode = Game_mode.MODE_NORMAL;
		actor = null;
		dragging = null;
		started_dragging = -1;
		actionchooser_created = false;
		action = null;
		i = new Item_factory();
		ma = new Monster_action_factory();
		ca = new Character_action_factory();
		flee_attempts = 0;
    }

	static void init(){
		prep();
		p = new Party();
		w = new World();
		e = new Enemies();
		p.reset(Campaignstate.get_party());
		w.reset(Campaignstate.mission_chosen);
		e.reset();
	}

	static void update(long ms){
		w.process(ms);
	}

	static void add_sound(String text){
		if (sound_queue!=null){
			sound_queue.add(text);
		}
	}

	static void add_message(String _text, String _image, int _color){
		Msg msg = new Msg(_text,_image,_color);
	    if (message_queue!=null){
	    	message_queue.add(msg);
	    }
	}

	static boolean dump(Context context, String filename){
		try {
	        FileOutputStream fileOut = context.openFileOutput(filename, Context.MODE_PRIVATE);
	        ObjectOutputStream out = new ObjectOutputStream(fileOut);
	        out.writeObject(p);
	        out.writeObject(w);
	        out.writeObject(e);
	        out.close();
	        fileOut.close();
	        return true;
		} catch (FileNotFoundException ex) {
    	    Log.e("Demo","crapped10");
            return false;
        } catch (IOException ex) {
     	    Log.e("Demo","crapped11");
      	    ex.printStackTrace();
            return false;
        }
	}

	static boolean load(Context context, String filename){
       try{
           FileInputStream fileIn = context.openFileInput(filename);
           ObjectInputStream in = new ObjectInputStream (fileIn);
           p = (Party)in.readObject();
           w = (World)in.readObject();
           e = (Enemies)in.readObject();
           in.close();
           fileIn.close();
           return true;
        } catch (FileNotFoundException ex) {
     	   Log.e("Demo","crapped12");
    	   return false;
        } catch (IOException ex) {
    	   Log.e("Demo","crapped13");
    	   return false;
        } catch (ClassNotFoundException ex) {
    	   Log.e("Demo","crapped14");
    	   return false;
        }
	}

}
